package pharao.client.views.game.landkarte.ort.training;


import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.Widget;

import pharao.client.i18ln.GuiText;
import pharao.client.views.events.GetModelDataEvent;
import pharao.client.views.events.SetModelDataEvent;
import pharao.client.views.game.landkarte.ort.basics.events.ShowOrtBasicViewEvent;
import pharao.client.views.game.landkarte.ort.training.events.AddTrainingResultEvent;
import pharao.client.views.game.landkarte.ort.training.events.GetTrainingLessonsEvent;
import hmvc4gwt.client.event.IHmvcEvent;
import hmvc4gwt.framework.client.events.CloseViewEvent;
import hmvc4gwt.framework.client.view.GuiView;

public class TrainingView extends GuiView {
	
	private Training training;
	
	private Hyperlink hyperlinkBack = new Hyperlink(GuiText.constants.link_back(), null);
	
	private Button buttonNext = new Button();
	
	/**
	 * prüfen Button
	 */
	private Button buttonCheck = new Button();

	public void initImpl() {
		
		getController().register(SetModelDataEvent.class, this);
		getController().fireEventLocal(new GetModelDataEvent());
		
		ClickListener listener = new ClickListener() {
			public void onClick(Widget sender) {
				hyperlinkAction(sender);
			}
		};
		
		hyperlinkBack.addClickListener(listener);
		
		
		buttonCheck.addClickListener(listener);
		buttonCheck.setText(GuiText.constants.button_game_map_ort_training_checkResults());
		
		buttonNext.addClickListener(listener);
		buttonNext.setText(GuiText.constants.button_game_training_next());

		add(hyperlinkBack);
	}
	
	private void hyperlinkAction(Widget sender) {
		
		if (sender == hyperlinkBack) {
			getController().fireEventLocal(new CloseViewEvent());
			getController().fireEvent(new ShowOrtBasicViewEvent(), true, false, false);
			
			resetView();
		} else if (sender == buttonCheck) {
			checkResult();
		} else if (sender == buttonNext) {
			nextTraining();
		} 
		
	}

	private void nextTraining() {
		remove(buttonNext);
		remove(getTraining());
		getController().fireEvent(new GetTrainingLessonsEvent(), true, false, false);
	}

	private void checkResult() {
		
		remove(buttonCheck);
		
		training.checkEntrys();
		
		getController().fireEvent(new AddTrainingResultEvent(getTraining().getListLessons()), true, false, false);
		
		add(buttonNext);
		
	}

	protected void resetView() {
		clear();
		add(hyperlinkBack);
	}
	
	public void terminate() {
		// TODO Auto-generated method stub

	}

	public void handleEventImpl(IHmvcEvent event) {
		
		if (event instanceof SetModelDataEvent) {
			
			if (event.getPayload() instanceof Training) {
				Training training = (Training) event.getPayload();
				
				setTraining(training);
				add(training);
				
				add(buttonCheck);
			}
			
		}
	}

	private Training getTraining() {
		return training;
	}

	private void setTraining(Training training) {
		this.training = training;
	}

}
